package com.example.shopping.login.controller;

import com.example.shopping.JsonResult;
import com.example.shopping.login.Util.JwtUtil;
import com.example.shopping.login.entity.*;
import com.example.shopping.login.entity.Collection;
import com.example.shopping.login.mapper.LoginMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@CrossOrigin
@RestController
public class LoginController {
    @Autowired
    LoginMapper loginMapper;

    //登录
    @RequestMapping("/api/login")
    public JsonResult getLogin(String account,String password) {
        List<Login> logins = loginMapper.getLogin(account,password);
        if(logins.isEmpty()){
            JsonResult ret = new JsonResult(false,"登录失败",1);
            return ret;
        }else{
            String token = JwtUtil.make(logins);
            Object[] dx=new Object[]{logins,token};

            JsonResult ret = new JsonResult(true,"登录成功",dx);
            return ret;
        }                                                                                                           

    }


    //数据统计
    @RequestMapping("/api/datastatistics")
    public JsonResult getDatastatistics() {
        ArrayList   <Shujutongji> shuju=new  ArrayList<Shujutongji>();
        shuju.add(new Shujutongji("2022-02-09","3000","长沙"));
        shuju.add(new Shujutongji("2022-02-10","3100","长沙"));
        shuju.add(new Shujutongji("2022-02-11","3200","长沙"));
        shuju.add(new Shujutongji("2022-02-12","3300","长沙"));
        shuju.add(new Shujutongji("2022-02-13","3400","长沙"));
        shuju.add(new Shujutongji("2022-02-09","700","湘潭"));
        shuju.add(new Shujutongji("2022-02-10","800","湘潭"));
        shuju.add(new Shujutongji("2022-02-11","900","湘潭"));
        shuju.add(new Shujutongji("2022-02-12","1000","湘潭"));
        shuju.add(new Shujutongji("2022-02-13","1200","湘潭"));
        shuju.add(new Shujutongji("2022-02-09","2000","岳阳"));
        shuju.add(new Shujutongji("2022-02-10","2100","岳阳"));
        shuju.add(new Shujutongji("2022-02-11","2200","岳阳"));
        shuju.add(new Shujutongji("2022-02-12","2300","岳阳"));
        shuju.add(new Shujutongji("2022-02-13","2400","岳阳"));
        shuju.add(new Shujutongji("2022-02-09","3500","南昌"));
        shuju.add(new Shujutongji("2022-02-10","3600","南昌"));
        shuju.add(new Shujutongji("2022-02-11","3700","南昌"));
        shuju.add(new Shujutongji("2022-02-12","3800","南昌"));
        shuju.add(new Shujutongji("2022-02-13","3900","南昌"));
        shuju.add(new Shujutongji("2022-02-09","4000","萍乡"));
        shuju.add(new Shujutongji("2022-02-10","4100","萍乡"));
        shuju.add(new Shujutongji("2022-02-11","4200","萍乡"));
        shuju.add(new Shujutongji("2022-02-12","4300","萍乡"));
        shuju.add(new Shujutongji("2022-02-13","4400","萍乡"));
        if(shuju.isEmpty()){
            JsonResult ret = new JsonResult(false,"查询失败",1);
            return ret;
        }else{
            JsonResult ret = new JsonResult(true,"查询成功",shuju);
            return ret;
        }

    }



    //轮播图
    @RequestMapping("/api/slideshow")
    public JsonResult getSlideShow() {
        List<SlideShow> slide = loginMapper.getSlideShow();
        if(slide.isEmpty()){
            JsonResult ret = new JsonResult(false,"查询失败",1);
            return ret;
        }else{
            JsonResult ret = new JsonResult(true,"查询成功",slide);
            return ret;
        }

    }


    //添加购物车
    @RequestMapping("/api/gouwuche/add")
    public JsonResult AddGouwuGoods(int userid,int productid,String goodsname,Double goodsprice,String goodsimg) {

        int ok=loginMapper.CartADD(userid,productid,goodsname,goodsprice,goodsimg);
        System.out.println(userid);
        System.out.println(productid);
        System.out.println(goodsname);
        System.out.println(goodsprice);
        System.out.println(goodsimg);
        JsonResult jsonResult = new JsonResult(true, "添加成功购物车", ok);
        if (ok == 1) {
            return jsonResult;
        } else {
            return new JsonResult(false, "添加失败", null);
        }
    }
    //用户查询收藏
    @RequestMapping("/api/gouwus/getcollectionid")
    public JsonResult getCollectionIdList(int userid,int productid) {
        List<Collection> collections = loginMapper.getCollectionIdList(userid,productid);
        System.out.println(userid);
        return new JsonResult(true, "查询成功", collections);
    }

    //删除收藏
    @RequestMapping("/api/Collections/dele")
    public JsonResult DeleCollection(int id) {
        int i = loginMapper.DeleCollection(id);
        JsonResult jsonResult = new JsonResult(true, "删除成功", i);
        if (i == 1) {
            return jsonResult;
        } else {
            return new JsonResult(false, "删除失败", null);
        }
    }

    //添加收藏
    @RequestMapping("/api/Collections/add")
    public JsonResult AddCollection(int id,int productid,int userid) {
        int i = loginMapper.AddCollection(id,productid,userid);
        System.out.println(id);
        System.out.println(productid);
        System.out.println(userid);
        JsonResult jsonResult = new JsonResult(true, "添加成功", i);
        if (i == 1) {
            return jsonResult;
        } else {
            return new JsonResult(false, "添加失败", null);
        }
    }

    //查询购物车全部商品
    @RequestMapping("/api/Collection/cartfind")
    public JsonResult getCartFind(int userid) {
        List<Cart> i = loginMapper.getCartFind(userid);
        JsonResult jsonResult = new JsonResult(true, "查询成功", i);
        return jsonResult;
    }

    //查询购物车全部商品
    @RequestMapping("/api/Collection/cartupdate")
    public JsonResult getCartUpdate(int quantity,int userid,int productid) {
        int i = loginMapper.getCartUpdate(quantity,userid,productid);
        JsonResult jsonResult = new JsonResult(true, "修改成功", i);
        return jsonResult;
    }

    //添加用户订单


    //用唯一标识查询用户
    @RequestMapping("/api/login/getusercloudid")
    public JsonResult getUserstateusercloudid(String usercloudid) {
        Userstate userstate = loginMapper.getUserstateusercloudid(usercloudid);
        if (userstate==null) {
            return new JsonResult(false, "查询失败", null);
        } else {
            return new JsonResult(true, "查询成功", userstate);
        }
    }

    //添加用户
    @RequestMapping("/api/userstate/add")
    public JsonResult AddUserstate(Userstate userstate) {
        int i = loginMapper.AddUserstate(userstate);
        JsonResult jsonResult = new JsonResult(true, "添加成功", i);
        if (i == 1) {
            return jsonResult;
        } else {
            return new JsonResult(false, "添加失败", null);
        }
    }

    //修改用户userid
    @RequestMapping("/api/userstate/up")
    public JsonResult UpUserstate(Userstate userstate) {
        int i = loginMapper.UpUserstate(userstate);
        JsonResult jsonResult = new JsonResult(true, "修改成功", i);
        if (i == 1) {
            return jsonResult;
        } else {
            return new JsonResult(false, "修改失败", null);
        }
    }
}
